var typeSelect1, typeSelect2, typeSelect3,ue;

$(function () {

    window.UEDITOR_CONFIG.serverUrl = "/good/serverurl.do";
    window.UEDITOR_CONFIG.imageUrl = "/good/serverurl1111.do";
    ue = UE.getEditor('ueditorcontainer',{
        initialFrameHeight:500,
        imageActionName:"upLoadImage",
        imageUrlPrefix:""
    });

    $("#goodPhoto").uploadify({
        height: 15,
        swf: ctx+'/home/assets/uploadify/img/uploadify.swf',
        uploader: ctx+'/good/add.do',
        buttonText: '选择文件',
        'fileTypeExts': '*.gif; *.jpg; *.jpeg; *.bmp',
        width: 60,
        auto: false,
        fileSizeLimit: '2MB',
        'fileObjName':'file',
        queueSizeLimit: 1,
        onInit: function () {
            $('.uploadify-button').addClass('btn btn-primary').css("-webkit-box-sizing", "content-box").css("box-sizing", "content-box");
            $('.uploadify-button').siblings('object').css("height", $('.uploadify-button').outerHeight());
            $('.uploadify-button').siblings('object').css("width", $('.uploadify-button').outerWidth());
        }
    });

    typeSelect1 = new Dropkick("#typeSelect1", {
        change: function () {
            while (typeSelect2.options.length > 1)
                typeSelect2.remove(0);
            while (typeSelect3.options.length > 1)
                typeSelect3.remove(0);
            var text = $(typeSelect1.selectedOptions).text();
            if (text != "请选择") {
                $.ajax({
                    url: ctx + '/goodType/getGoodTypeList.do',
                    dataType: 'json',
                    type: 'post',
                    data: {parentId: typeSelect1.value},
                    success: function (data) {
                        for (var i = 0; i < data.length; i++)
                            typeSelect2.add($('<option value ="' + data[i].ID + '">' + data[i].NAME + '</option>')[0], typeSelect2.options.length - 1);
                    }
                });
            }
        }
    });
    typeSelect2 = new Dropkick("#typeSelect2", {
        change: function () {
            while (typeSelect3.options.length > 1)
                typeSelect3.remove(0);
            var text = $(typeSelect2.selectedOptions).text();
            if (text != "请选择") {
                $.ajax({
                    url: ctx + '/goodType/getGoodTypeList.do',
                    dataType: 'json',
                    type: 'post',
                    data: {parentId: typeSelect2.value},
                    success: function (data) {
                        for (var i = 0; i < data.length; i++)
                            typeSelect3.add($('<option value ="' + data[i].ID + '">' + data[i].NAME + '</option>')[0], typeSelect3.options.length - 1);
                    }
                });
            }
        }
    });
    typeSelect3 = new Dropkick("#typeSelect3", {
        change: function () {
            var text = $(typeSelect3.selectedOptions).text();
            if (text != "请选择") {
                $.ajax({
                    url: ctx + '/goodTypeProperty/getTypePropertyTree.do',
                    dataType: 'json',
                    type: 'post',
                    data: {goodTypeId: typeSelect3.value},
                    success: function (data) {
                        $('.shopPropertyCheckContainer').html("");
                        if(data.length == 0){
                            $('.shopPropertyCheckContainer').attr('style','');
                            return;
                        }
                        $('.shopPropertyCheckContainer').css("max-height","230px");
                        $('.shopPropertyCheckContainer').css("overflow","auto");
                        $('.shopPropertyCheckContainer').css("height","auto");
                        $('.shopPropertyCheckContainer').css("border","1px solid #CCCCCC");
                        $('.shopPropertyCheckContainer').css("border-radius","5.6px");
                        for (var i = 0; i < data.length; i++) {
                            if(i!=0)
                                $('.shopPropertyCheckContainer').append('<div style="float:left;width:10%;margin-top: 10px;">' + data[i].name + '</div><div style="float:left;width:90%;margin-top: 10px;"></div>');
                            else
                                $('.shopPropertyCheckContainer').append('<div style="float:left;width:10%;">' + data[i].name + '</div><div style="float:left;width:90%;"></div>');
                            for (var j = 0; j < data[i].values.length; j++) {
                                $('.shopPropertyCheckContainer>div:last').append('<div style="display:inline-block;">' +
                                    '<input id="propertyValue' + data[i].values[j].id + '" type="checkbox" name="propertyValue" value="' + data[i].values[j].id + '">' +
                                    '<span style="margin-right: 20px;margin-left: 6px;">' + data[i].values[j].name + '</span>' +
                                    '</div>');
                            }
                        }
                        $('.shopPropertyCheckContainer').append('<div class="clear-float"></div>');
                        $('.shopPropertyCheckContainer').append("<script>$('input').iCheck({checkboxClass: 'icheckbox_square-blue',radioClass: 'iradio_square-blue',increaseArea: '20%'});</script>");
                    }
                });
            } else {
                $('.shopPropertyCheckContainer').attr('style','');
                $('.shopPropertyCheckContainer').html('<span class="shop-tip">（请先选择类型）</span>');
            }
        }
    });

    $.ajax({
        url: ctx + '/goodType/getGoodTypeList.do',
        dataType: 'json',
        type: 'post',
        data: {},
        success: function (data) {
            for (var i = 0; i < data.length; i++)
                typeSelect1.add($('<option value ="' + data[i].ID + '">' + data[i].NAME + '</option>')[0], typeSelect1.options.length - 1);
        }
    });

    $("#submitAddGood").click(function(){
        var text = $(typeSelect3.selectedOptions).text();
        if (text == "请选择") {
            alert('请先选择商品类型');
            return;
        }
        if($('#goodName').val()==''){
            alert('请填写商品标题');
            return;
        }
        $('#goodPrice').val(parseFloat($('#goodPrice').val())?parseFloat($('#goodPrice').val()):"");
        if($('#goodPrice').val()==''){
            alert('请填写价格');
            return;
        }
        $('#goodStore').val(parseInt($('#goodStore').val())?parseInt($('#goodStore').val()):"");
        if($('#goodStore').val()==''){
            alert('请填写库存');
            return;
        }
        if($("#goodPhoto").data('uploadify').queueData.queueLength==0){
            alert('请上传商品图片');
            return;
        }
        var propertyValues = "";
        $("[name='propertyValue']:checked").each(function(){
            propertyValues = propertyValues+","+$(this).val();
        });
        propertyValues = propertyValues.substr(1)
        var data = {
            name:$('#goodName').val(),
            type:typeSelect3.value,
            price:$('#goodPrice').val(),
            stock:$('#goodStore').val(),
            describe:ue.getContent(),
            propertyValues:propertyValues
        };

        $("#goodPhoto").uploadify("settings", "formData", data);
        $("#goodPhoto").uploadify("settings", "onQueueComplete", function(){
            alert("添加成功");
        });
        $('#goodPhoto').uploadify('upload','*');//批量上传
    });
});